package cn.myzf.douyin;

import cn.myzf.chatapi.WeChatClient;
import cn.myzf.chatapi.wechat.utils.WxClientRel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

import static cn.myzf.chatapi.WeChatDemo.LISTENER;

/**
 * @package cn.myzf.douyin
 * @Date Created in 2019/7/25 17:08
 * @Author myzf
 */

@Controller
@RequestMapping("api")
public class web {



    @Autowired
    private DyService dyService;



    @RequestMapping("index")
    public String index(){
        return "a";
    }


    @RequestMapping("initLog")
    public String initLog(Model model) throws InterruptedException {
        WeChatClient wechatClient = WeChatClient.getInstance();
        if(!WeChatClient.isLogin()){
            //为模拟微信客户端设置监听器
            wechatClient.setListener(LISTENER);
            //启动模拟微信客户端
            if(wechatClient.wxThread != null){
                wechatClient.stop();
            }
            wechatClient.startup();
            TimeUnit.SECONDS.sleep(2);
            String qrCode = (String) WxClientRel.get("qrCode");
            model.addAttribute("qrCode",qrCode);
        }
        else {
            model.addAttribute("notice","你已经登录了!");
        }


        return "login";
    }

    @RequestMapping("/realUrl")
    @ResponseBody
    public String[] realUrl(String url){
        if(StringUtils.isEmpty(url)){
            return new String[]{"错误"};
        }
        //优先请求别人的接口
        String[] realUrl = dyService.getRealUrl(url,null);
        return realUrl;
    }





}
